/**
 * 商户列表
 */
var pageCurr;
$(function() {
    layui.use('table', function(){
        var table = layui.table
            ,form = layui.form;

        tableIns=table.render({
            elem: '#shopList'
            ,url:'/shop/getShops'
        	,method: 'post' //默认：get请求
            ,cellMinWidth: 80
            ,page: true,
            request: {
                pageName: 'page' //页码的参数名称，默认：page
                ,limitName: 'limit' //每页数据量的参数名，默认：limit
            },response:{
                statusName: 'code' //数据状态的字段名称，默认：code
                ,statusCode: 200 //成功的状态码，默认：0
                ,countName: 'totals' //数据总数的字段名称，默认：count
                ,dataName: 'list' //数据列表的字段名称，默认：data
            }
            ,cols: [[
                {type:'numbers'}
                ,{field:'id', title:'ID', width:80, unresize: true, sort: true,align:'center'}
                ,{field:'name', width:180, title:'商户名',align:'center'}
                ,{field:'mobile', width:180, title:'联系电话',align:'center'}
                ,{field:'isDel', title:'状态',width:120,align:'center',templet:'#isDelTpl'}
                ,{field:'insertTime', width:180, title: '添加时间',align:'center'}
                ,{field:'updateTime', width:180, title: '变更时间',align:'center'}
                ,{fixed:'right', title:'操作', width:140,align:'center', toolbar:'#optBar'}
            ]]
            ,  done: function(res, curr, count){
            }
        });

        //监听工具条
        table.on('tool(shopTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                delShop(data,data.id,data.name);
            } else if(obj.event === 'edit'){
                //编辑
                getShop(data,data.id);
            } else if(obj.event === 'recover'){
                //恢复
                recoverShop(data,data.id);
            }
        });
        //监听提交
        form.on('submit(shopSubmit)', function(data){
            // TODO 校验
            formSubmit(data);
            return false;
        });

    });
    //搜索框
    layui.use(['form','laydate'], function(){
        var form = layui.form ,layer = layui.layer
            ,laydate = layui.laydate;
        //日期
        laydate.render({
            elem: '#qinsertTimeStart'
        });
        laydate.render({
            elem: '#qinsertTimeEnd'
        });
        //TODO 数据校验
        //监听搜索框
        form.on('submit(searchSubmit)', function(data){
            //重新加载table
            load(data);
            return false;
        });
    });
});

//提交表单
function formSubmit(obj){
	submitAjax(obj);
}
function submitAjax(obj){
    $.ajax({
        type: "POST",
        data: $("#shopForm").serialize(),
        url: "/shop/setShop",
        success: function (data) {
            if(isLogin(data)){
                if (data == "ok") {
                    layer.alert("操作成功",function(){
                    	 layer.closeAll();
                         cleanShop();
                         //加载页面
                         load(obj); 
                    });
                } else {
                	layer.alert(data,function(){
                    	layer.closeAll();
                    	//加载load方法
                    	load(obj);//自定义
                    });
                }
            }
        },
        error: function () {
            layer.alert("操作请求错误，请您稍后再试",function(){
                layer.closeAll();
                //加载load方法
                load(obj);//自定义
            });
        }
    });
}

function cleanShop(){
	$("#name").val("");
	$("#mobile").val("");
}
//开通商户
function addUser(){
	openShop(null,"开通商户");
    //重新渲染下form表单 否则复选框无效
    layui.form.render('checkbox');
}
function openShop(id,title){
	if(id==null || id==""){
        $("#id").val("");
    }
    layer.open({
        type:1,
        title: title,
        fixed:false,
        resize :false,
        shadeClose: true,
        area: ['550px'],
        content:$('#setShop'),
        end:function(){
        	cleanShop();
        }
    });
}
function getShop(obj,id) {
	//如果已经离职，提醒不可编辑和删除
	if(obj.isDel){
        layer.alert("该商户已经删除，不可进行编辑；</br>  如需编辑，请先<font style='font-weight:bold;' color='blue'>恢复</font>商户状态。");
    }else{
	    //回显数据
	    $.get("/shop/getShop",{"id":id},function(data){
	    	if(isLogin(data)){
		        if(data.msg=="ok" && data.shop!=null){
		            $("#id").val(data.shop.id==null?'':data.shop.id);
		            $("#name").val(data.shop.name==null?'':data.shop.name);
		            $("#mobile").val(data.shop.mobile==null?'':data.shop.mobile);
		            openShop(id,"设置商户");
		        }else{
		            //弹出错误提示
		            layer.alert(data.msg,function () {
		                layer.closeAll();
		            });
		        }
	    	}
	    });
    }
}
function delShop(obj,id,name) {
    if(null!=id){
    	 layer.confirm('您确定要删除'+name+'商户吗？', {
	            btn: ['确认','返回'] //按钮
	        }, function(){
	            $.post("/shop/delShop",{"id":id},function(data){
	            	if(isLogin(data)){
		                if(data=="ok"){
		                    //回调弹框
		                    layer.alert("删除成功！",function(){
		                        layer.closeAll();
		                        //加载load方法
		                        load(obj);//自定义
		                    });
		                }else{
		                	layer.alert(data,function(){
		                    	layer.closeAll();
		                    	//加载load方法
		                    	load(obj);//自定义
		                    });
		                }
	            	}
	            });
	        }, function(){
	            layer.closeAll();
	        });
    }
}
function recoverShop(obj,id) {
    if(null!=id){
        layer.confirm('您确定要恢复'+name+'商户吗？', {
            btn: ['确认','返回'] //按钮
        }, function(){
            $.post("/shop/recoverShop",{"id":id},function(data){
                if(isLogin(data)){
                    if(data=="ok"){
                        //回调弹框
                        layer.alert("恢复成功！",function(){
                            layer.closeAll();
                            //加载load方法
                            load(obj);//自定义
                        });
                    }else{
                    	layer.alert(data,function(){
                        	layer.closeAll();
                        	//加载load方法
                        	load(obj);//自定义
                        });
                    }
                }
            });
        }, function(){
            layer.closeAll();
        });
    }
}

function load(obj){
    //重新加载table
    tableIns.reload({
        where: obj.field
        , page: {
            curr: pageCurr //从当前页码开始
        }
    });
}

